package cn.tedu.zhyl.system.service.impl;

import cn.tedu.zhyl.base.constant.Constants;
import cn.tedu.zhyl.base.exception.ServiceException;
import cn.tedu.zhyl.base.response.PageResponse;
import cn.tedu.zhyl.base.response.StatusCode;
import cn.tedu.zhyl.base.utils.AuthContext;
import cn.tedu.zhyl.system.mapper.SysUserMapper;
import cn.tedu.zhyl.system.pojo.dto.LoginDTO;
import cn.tedu.zhyl.system.pojo.dto.SysUserParams;
import cn.tedu.zhyl.system.pojo.dto.SysUserQuery;
import cn.tedu.zhyl.system.pojo.entity.SysUser;
import cn.tedu.zhyl.system.pojo.vo.LoginVO;
import cn.tedu.zhyl.system.pojo.vo.SysUserVO;
import cn.tedu.zhyl.system.service.SysUserService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public LoginVO login(LoginDTO loginDTO) {
        // 校验用户名
        SysUser sysUser = sysUserMapper.findByUsername(loginDTO);

        // 校验账户
        if(sysUser == null) throw new ServiceException(StatusCode.USERNAME_NOT_EXIST);

        // 校验密码
        if(!loginDTO.getPassword().equals(sysUser.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }

        // 生成token
        String token = UUID.randomUUID().toString().replaceAll("-", "");

        // 存入redis
        redisTemplate.opsForValue().set(Constants.USER_LOGIN_API+token, JSON.toJSONString(sysUser),60, TimeUnit.MINUTES);


        // 封装返回对象
        LoginVO loginVO = new LoginVO();
        BeanUtils.copyProperties(sysUser,loginVO);
        loginVO.setToken(token);
//        loginVO.setUsername(sysUser.getUsername());

        return loginVO;
    }

    @Override
    public void logout(String token) {
        redisTemplate.delete(Constants.USER_LOGIN_API+token);
    }

    @Override
    public PageResponse<SysUserVO> findByPage(Integer page, Integer limit, SysUserQuery sysUserQuery) {
        PageHelper.startPage(page,limit);
        List<SysUserVO> pageList = sysUserMapper.findByQuery(sysUserQuery);
        return PageResponse.of(new PageInfo<>(pageList));

    }

    @Override
    public void updateUserStatus(Long uid, Integer status) {
        sysUserMapper.updateUserStatus(uid,status);
    }

    @Override
    public void resetPassword(Long uid) {
        sysUserMapper.resetPassword(uid);
    }

    @Override
    public void delete(Long uid) {
        sysUserMapper.deleteUser(uid);
    }

    @Override
    public void save(SysUserParams sysUserParams) {
        Long id = sysUserParams.getId();
        if(id == null){
            sysUserParams.setPassword("123456");
            sysUserMapper.insert(sysUserParams);
        }else {
            sysUserMapper.update(sysUserParams);
        }
    }
}
